<%@ include file="../common/base.jsp" %>

<%@ page import="com.octopus.kernal.controller.SearchOperator" %>

<div id="header">
<!-- -->
	<img width="54" height="57" src="<%=request.getContextPath() %>/img/logo.gif" alt="<fmt:message key='yangj' />">&nbsp;

	<fmt:message key="label.common.yangj" />
	&nbsp;&nbsp;
	<font size="3">v0.9</font>


	<div id="div_lang" style="float:right;font-size:12px;margin-top:40px;margin-right:10px;" >
		<a href="javascript:setPreLocale('en_EN')">English</a> | <a href="javascript:setPreLocale('zh_CN')"><fmt:message key='label.common.chinese' /></a>
	</div>
</div>

<script language="javascript">

var rows = document.getElementsByTagName('tr');
for (var i = 0; i < rows.length; i++) {
	rows[i].onmouseover = function() {
		this.className += ' hilite';
	}
	rows[i].onmouseout = function() {
		this.className = this.className.replace('hilite', '');
	}
}

function setPreLocale(locale){
	setCookie("preferLocale",locale);
	window.location.reload();
}

function hideMenu(){
	var cookie = getCookie("hideMenu");
	if(cookie != null){
		delCookie("hideMenu");
	}
	setCookie("hideMenu", "true");
	var menu = document.getElementById("leftside");
	var content = document.getElementById("rightside");
	var hma = document.getElementById("hma");
	var sma = document.getElementById("sma");
	menu.style.display = "none";
	//enlarge(860);
	content.style.width = "960px";
	hma.style.display = "none";
	sma.style.display = "block";
	if(document.getElementById("banner") != null){
		document.getElementById("banner").style.width = "960px";
	}
	if(document.getElementById("queryTable") != null){
		document.getElementById("queryTable").style.width = "940px";
	}
	if(document.getElementById("detailTable") != null){
		document.getElementById("detailTable").style.width = "940px";
	}
}

function enlarge(width){
	var cur = document.getElementById('rightside').style.width;
	if(cur == '960px'){
		return;
	}
	document.getElementById('rightside').style.width = width + "px";
	if(width == '960'){
		return;
	}
	setTimeout("enlarge(Number(document.getElementById('rightside').style.width.substr(0,3))+2)",1);
}

function deflate(width){
	var cur = document.getElementById('rightside').style.width;
	if(cur == '' || cur == '860px'){
		return;
	}
	document.getElementById('rightside').style.width = width + "px";
	if(width == '860'){
		menu = document.getElementById("leftside").style.display = "block";
		return;
	}
	setTimeout("deflate(Number(document.getElementById('rightside').style.width.substr(0,3))-2)",1);
}

function showMenu(){
	var cookie = getCookie("hideMenu");
	if(cookie != null){
		delCookie("hideMenu");
	}
	setCookie("hideMenu", "false");
	var menu = document.getElementById("leftside");
	var content = document.getElementById("rightside");
	var hma = document.getElementById("hma");
	var sma = document.getElementById("sma");
	//deflate(960);
	menu.style.display = "block";
	content.style.width = "840px";
	hma.style.display = "block";
	sma.style.display = "none";
	if(document.getElementById("banner") != null){
		document.getElementById("banner").style.width = "838px";
	}
	if(document.getElementById("queryTable") != null){
		document.getElementById("queryTable").style.width = "820px";
	}
	if(document.getElementById("detailTable") != null){
		document.getElementById("detailTable").style.width = "820px";
	}
}

function setCookie(name,value){
    var Days = 3650;
    var exp  = new Date();
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = name + "="+ escape (value) + ";path=/;expires=" + exp.toGMTString();
}

function getCookie(name){
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
    if(arr != null) return unescape(arr[2]); return null;
}

function delCookie(name){
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null) document.cookie= name + "="+cval+";path=/;expires="+exp.toGMTString();
}

function pickSelected(select1){
      var newvalue = prompt("<fmt:message key='label.common.please.input' />","");
      if(newvalue){
         addSelected(select1,newvalue);
      }
	}

function addSelected(fld1,text1){
	var arr = text1.split("");
	var text2 = arr.join(".*");
	var flag = true;
	var key;
	var reg = new RegExp(text2,"i");
	for(var i = 0; i < fld1.options.length; i++){
		if(fld1.options[i].disabled == true){
			continue;
		}
		if(fld1.options[i].text.match(reg)){
			key = fld1.options[i].value;
			fld1.options[i].selected = true;
			flag = false;
			break;
		}
	}
	if(flag == true){
		alert("<fmt:message key='label.common.value.notexist' />");
		return;
	}
}

function addSelected2(fld1,text1){
	var arr = text1.split("");
	var text2 = arr.join(".*");
	var flag = true;
	var key;
	var reg = new RegExp(text2,"i");
	for(var i = 0; i < fld1.options.length; i++){
		fld1.options[i].selected = false;
		if(fld1.options[i].disabled == true){
			continue;
		}
		if(fld1.options[i].text.match(reg)){
			key = fld1.options[i].text;
			var begin = key.indexOf('[') + 1; 
			var end = key.indexOf(']'); 
			key = key.substring(begin,end); 
			fld1.options[i].selected = true;
			return key;
		}
	}
	if(flag == true){
		alert("<fmt:message key='label.common.value.notexist' />");
		return "none";
	}
}

function pageX(elem) { 
	return elem.offsetParent?(elem.offsetLeft+pageX(elem.offsetParent)):elem.offsetLeft; 
} 

function pageY(elem) { 
	return elem.offsetParent?(elem.offsetTop+pageY(elem.offsetParent)):elem.offsetTop; 
} 

function onAutoFillPosition(components){
	var ids = components.split(",");
	for(var i = 0; i < ids.length; i++){
		id = ids[i];
		var select = document.getElementById(id);
		var input = document.getElementById(id + "_af");
		input.style.left=pageX(select) + "px";
		if(!+[1,]){
			input.style.top=pageY(select) + "px";
		}else{
			input.style.top=pageY(select) - 3 + "px";
		}
	}
}

function onAutoFillValue(id){
	var select = document.getElementById(id);
	var input = document.getElementById(id + "_af");
	if(input){
		input.value = select.options[select.selectedIndex].text;
	}
}

function onAutoFill(id){
	var select = document.getElementById(id);
	var select_bak = document.getElementById(id + "_bak");
	var input = document.getElementById(id + "_af");
	if(input){
		addSelected(select, input.value);
		onAutoFillValue(id);
		select.onchange();
	}
}

function onAutoFillCascade(id, cascades){
	var parents = cascades.split(",");
	var select_bak = document.getElementById(id + "_bak");
	var input = document.getElementById(id + "_af");
	if(input){
		var parentId = addSelected2(select_bak, input.value);
		if(parentId == "none"){
			return;
		}
		for(var i = 0; i < parents.length; i++){
			var select = document.getElementById(parents[i] + "_bak");
			for(var j = 1; j < select.options.length; j++){
				if(select.options[j].value == parentId){
					select.options[j].selected = true;
					if(select.options[j].text.indexOf('[') > 0){
						parentId = select.options[j].text;
						var begin = parentId.indexOf('[') + 1; 
						var end = parentId.indexOf(']'); 
						parentId = parentId.substring(begin,end);
					}
					break;
				}else{
					select.options[j].selected = false;
				}
			}
		}
		var top = document.getElementById(parents[parents.length - 1]);
		for(var i = 1; i < top.options.length; i++){
			if(top.options[i].value == parentId){
				top.options[i].selected = true;
				top.value = parentId;
				break;
			}else{
				top.options[i].selected = false;
			}
		}
		for(var i = parents.length; i > 0; i--){
			var select = document.getElementById(parents[i - 1]);
			select.onchange();
		}
	}
}

function onSelectCascade(select0Id, select1Id){
	var select0 = document.getElementById(select0Id);
	var select1 = document.getElementById(select1Id);
	var select1_bak = document.getElementById(select1Id + "_bak");
	var oldoptions = select1.options;
	var oldsize = oldoptions.length;
	for(var i = 1; i < oldsize; i++){
		select1.removeChild(oldoptions[1]); 
	}
	var trigger = select0.value;
	for(var i = 1; i < select1_bak.options.length; i++){
		var prefix = select1_bak.options[i].text;
		var begin = prefix.indexOf('[') + 1; 
		var end = prefix.indexOf(']'); 
		prefix = prefix.substring(begin,end); 
		var text = select1_bak.options[i].text.substring(0, begin-1);
		var value = select1_bak.options[i].value;
		if(prefix == trigger){
			var option = new Option(text,value);
			option.selected = select1_bak.options[i].selected;
			select1.options.add(option);
		}
	}
	onAutoFillValue(select1Id);
}

function onCreate(){
	var offset = request('pager.offset');
	if(offset == undefined){
		window.location.href = "./create.o";
	}else{
		window.location.href = "./create.o?&pager.offset=" + offset;
	}
}

function onEdit(id){
	var offset = request('pager.offset');
	if(offset == undefined){
		window.location.href = "./update.o?id=" + id;
	}else{
		window.location.href = "./update.o?id=" + id + "&pager.offset=" + offset;
	}
}

function onView(id){
	var offset = request('pager.offset');
	if(offset == undefined){
		window.location.href = "./view.o?id=" + id;
	}else{
		window.location.href = "./view.o?id=" + id + "&pager.offset=" + offset;
	}
}

function onCancel(){
	var offset = request('pager.offset');
	if(offset == undefined){
		window.location.href = "./query.o";
	}else{
		window.location.href = "./query.o?pager.offset=" + offset;
	}
}

function onDelete(id){
	if(window.confirm("<fmt:message key='label.common.confirm.delete' />")){
		var offset = request('pager.offset');
		if(offset == undefined){
			window.location.href = "./delete.o?id=" + id;
		}else{
			window.location.href = "./delete.o?id=" + id + "&pager.offset=" + offset;
		}
	}
}

function onActionGet(action, id, confirmmsg){
	if(confirmmsg != ""){
		if(!window.confirm(confirmmsg)){
			return;
		}
	}
	var offset = request('pager.offset');
	if(offset == undefined){
		window.location.href = action + ".o?" + (id==""?"":"id=" + id);
	}else{
		window.location.href = action + ".o?" + (id==""?"":"id=" + id + "&") + "pager.offset=" + offset;
	}
}

function onActionPost(action, confirmmsg){
	if(confirmmsg != ""){
		if(!window.confirm(confirmmsg)){
			return;
		}
	}
	document.getElementById("ACTION").value = action;
	document.forms[0].submit();
}

String.prototype.replaceAll  = function(s1,s2){   
	return this.replace(new RegExp(s1,"gm"),s2);   
}  

function onSort(field,way){
	var sort = "";
	if(way.length > 0){
		sort = "&field.sort=" + field + ":" + way;
	}
	window.location.href = window.location.href.split("?")[0] + "?pager.offset=0" + sort;
}

function onCheckboxDisable(checkbox,element){
	if(checkbox.checked){
		document.getElementById(element).disabled = false;
	}else{
		document.getElementById(element).disabled = true;
	}
}

function onOperatorChange(select, element){
	if(select.value == ""){
		document.getElementById("entity." + element).disabled = true;
		if(document.getElementById("pick.entity." + element) != null){
			document.getElementById("pick.entity." + element).removeAttribute('href');
		}
		if(document.getElementById("entity2." + element) != null){
			document.getElementById("entity2." + element).disabled = true;
		}
		if(document.getElementById("entity2." + element + "Str") != null){
			document.getElementById("entity2." + element + "Str").disabled = true;
		}
		if(document.getElementById("entity2." + element + "Id") != null){
			document.getElementById("entity2." + element + "Id").disabled = true;
		}
	}else if(select.value == <%=SearchOperator.EQUAL.ordinal()%> 
		|| select.value == <%=SearchOperator.NOTEQUAL.ordinal()%> 
		|| select.value == <%=SearchOperator.LIKE.ordinal()%> 
		|| select.value == <%=SearchOperator.GREATER.ordinal()%> 
		|| select.value == <%=SearchOperator.LESS.ordinal()%>){
			document.getElementById("entity." + element).disabled = false;
			if(document.getElementById("pick.entity." + element) != null){
				document.getElementById("pick.entity." + element).href = "javascript:pickSelected(document.getElementById('entity." + element + "'));";
			}
			if(document.getElementById("entity2." + element) != null){
				document.getElementById("entity2." + element).disabled = true;
			}
			if(document.getElementById("entity2." + element + "Str") != null){
				document.getElementById("entity2." + element + "Str").disabled = true;
			}
			if(document.getElementById("entity2." + element + "Id") != null){
				document.getElementById("entity2." + element + "Id").disabled = true;
			}
	}else if(select.value == <%=SearchOperator.BETWEEN.ordinal()%>){
		document.getElementById("entity." + element).disabled = false;
		if(document.getElementById("pick.entity." + element) != null){
			document.getElementById("pick.entity." + element).href = "javascript:pickSelected(document.getElementById('entity." + element + "'));";
		}
		if(document.getElementById("entity2." + element) != null){
			document.getElementById("entity2." + element).disabled = false;
		}
		if(document.getElementById("entity2." + element + "Str") != null){
			document.getElementById("entity2." + element + "Str").disabled = false;
		}
		if(document.getElementById("entity2." + element + "Id") != null){
			document.getElementById("entity2." + element + "Id").disabled = false;
		}
	}	
}

function request(strParame) { 
	var args = new Object( ); 
	var query = location.search.substring(1); 
	var pairs = query.split("&"); // Break at ampersand 
	for(var i = 0; i < pairs.length; i++) { 
		var pos = pairs[i].indexOf('='); 
		if (pos == -1) continue; 
		var argname = pairs[i].substring(0,pos); 
		var value = pairs[i].substring(pos+1); 
		value = decodeURIComponent(value); 
		args[argname] = value; 
	} 
	return args[strParame]; 
} 

</script>